package post;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Post {

    private Store store;
    private Cashier cashier;

    Post(Store s, Cashier c) throws IOException {
        this.cashier = c;
        this.store = s;

        commandLinePrompt();
    }
    
    void commandLinePrompt() throws IOException
    {
        System.out.println("Welcome to the 668 POST SYSTEM");
        System.out.println("Here are our items:");
        System.out.println("Banana: $1.00, UPC = 1234");
        System.out.println("Apple: $0.25, UPC = 4321");
        System.out.println("Benadryl: $20.00, UPC = 5678");
        System.out.println("Computer Science Degree: $13,000, UPC = 8765");

        ArrayList<SalesLineItem> groceryList = new ArrayList<SalesLineItem>();
        String userInput = "";

        BufferedReader src = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Enter f to finish");
        System.out.println("Enter the UPC of the item and the quantity (e.g., 8765 1):");
        
        String upc = "";

        do {
            try {
                // TODO add validation code before inserting into the Grocery List
                StringTokenizer validate = new StringTokenizer(src.readLine());
                upc = validate.nextToken();
                
                if(!upc.equals("f")) {
                    Integer quantity = Integer.parseInt(validate.nextToken());
                    Item dummyItem = this.store.getProductCatalog().getItemByUpc(upc);

                    groceryList.add(new SalesLineItem(quantity, dummyItem));
                    src.readLine();
                }
            } catch (IOException ex) {
                Logger.getLogger(Post.class.getName()).log(Level.SEVERE, null, ex);
            }
        } while(!upc.equals("f"));
        
        double totalPrice = 0.0;
        
        for(int i = 0; i < groceryList.size(); i++) {
            totalPrice += groceryList.get(i).getPrice();
        }
        
        System.out.print("Payment - Cash, Credit, Check:");
        String paymentPreference = src.readLine();
        System.out.println("You chose to pay by: " + paymentPreference + ".");
        System.out.print("Enter amount to pay (e.g: 100.00): ");
        Float cash = Float.parseFloat(src.readLine());
        // Print invoice
    }
}
